<?php



namespace App\Services\Company\Services;

use App\Exceptions\ServiceException;
use App\Services\Member\Interfaces\UserServiceInterface;
use App\Services\Member\Models\User;
use App\Services\Member\Models\UserInviteBalanceRecord;
use App\Services\Member\Models\UserInviteBalanceWithdrawOrder;
use App\Services\Company\Interfaces\CompanyBalanceServiceInterface;
use App\Services\Company\Interfaces\CompanyServiceInterface;
use App\Services\Company\Models\CompanyBalanceRecord;
use App\Services\Company\Models\CompanyBalanceWithdrawOrder;
use Illuminate\Support\Facades\Auth;

class CompanyBalanceService implements CompanyBalanceServiceInterface
{

    /**
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public function simplePaginate(int $page, int $pageSize): array
    {
        $query = UserInviteBalanceRecord::whereUserId(Auth::id())->latest();

        $total = $query->count();
        $list = $query->forPage($page, $pageSize)->get()->toArray();

        return compact('list', 'total');
    }

    /**
     * @param int $userId
     * @param int $reward
     */
    public function createInvite(int $userId, int $reward): void
    {
        if ($reward == 0) {
            return;
        }
        UserInviteBalanceRecord::create([
            'user_id' => $userId,
            'type' => UserInviteBalanceRecord::TYPE_DEFAULT,
            'total' => $reward,
            'desc' => __('邀请奖励'),
            'is_type'=>1
        ]);
        User::find($userId)->increment('invite_balance', $reward);
    }

    /**
     * @param int $userId
     * @param int $orderTotal
     * @param array $order
     */
    public function createBalance(int $userId, int $orderTotal, array $order): void
    {
        if ($orderTotal == 0) {
            return;
        }
        CompanyBalanceRecord::create([
            'user_id' => $userId,
            'company_id'=>$order['company_id'],
            'total' => $orderTotal,
            'order_id'=>$order['id'],
            'desc' => __('订单结算'),

        ]);
        /**
         * @var CompanyService $companyService
         */
        $companyService = app()->make(CompanyServiceInterface::class);
        $companyService->balanceInc($order['company_id'], $orderTotal);
    }

    /**
     * @param int $total
     * @param array $account
     * @throws ServiceException
     */
    public function     createCurrentCompanyWithdraw(int $total, int $accountId,string $remark): void
    {

        /**
         * @var CompanyService $companyService
         */
        $companyService = app()->make(CompanyServiceInterface::class);

        $shop = $companyService->currentCompany();
        if ($shop['balance'] < $total) {
            throw new ServiceException(__('提现余额有误'));
        }
        // 扣除余额
        $companyService->balanceWithdrawalInc($shop['id'], -$total);


        // 创建提现订单
        CompanyBalanceWithdrawOrder::create([
            'company_id' => $shop['id'],
            'total' => $total,
            'before_balance' => $shop['balance'] - $total,
            'account_id'=>$accountId,
            'status'=>CompanyBalanceWithdrawOrder::STATUS_DEFAULT,
            'remark'=>$remark,
        ]);

     //   event(new UserInviteBalanceWithdrawCreatedEvent($user['id'], $order['id']));
    }

    /**
 * @param int $page
 * @param int $size
 * @return array
 */
    public function currentCompanyOrderPaginate(int $page, int $size): array
    {
        $user = Auth::user();
        $query = CompanyBalanceWithdrawOrder::query()->with(['account'])->where('company_id',$user['login_company_id'])->latest();

        $total = $query->count();
        $list = $query->forPage($page, $size)->get()->toArray();

        return compact('list', 'total');
    }

    /**
     * @param int $page
     * @param int $size
     * @return array
     */
    public function currentCompanyBalancePaginate(int $shopId,int $page, int $size): array
    {
        $query = CompanyBalanceRecord::query()->with(['user','order'])->where('company_id',$shopId)->latest();

        $total = $query->count();
        $list = $query->forPage($page, $size)->get()->toArray();

        return compact('list', 'total');
    }


    /**
     * @param array $ids
     * @return array
     */
    public function getOrdersList(array $ids): array
    {
        return UserInviteBalanceWithdrawOrder::whereIn('id', $ids)->get()->toArray();
    }

    /**
     * @param array $order
     */
    public function withdrawOrderRefund(array $order): void
    {
        // 余额记录
        UserInviteBalanceRecord::create([
            'user_id' => $order['user_id'],
            'type' => UserInviteBalanceRecord::TYPE_ORDER_WITHDRAW_BACK,
            'total' => $order['total'],
            'desc' => __('提现失败退还'),
            'is_type'=>1
        ]);
        // 扣除余额
        app()->make(UserServiceInterface::class)->inviteBalanceInc($order['user_id'], $order['total']);
    }
}
